package com.xbs.controller.content;

import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.xbs.controller.base.BaseServlet;
import com.xbs.entity.CarModelEntity;
import com.xbs.entity.CommonOrderEntity;
import com.xbs.entity.OrderMoney;
import com.xbs.entity.RentEntity;
import com.xbs.entity.TimeEntity;
import com.xbs.service.content.OrderService;
import com.xbs.service.selfdriving.SelfDrivingService;
import com.xbs.utils.TimeUtils;

@WebServlet("/content/order")
public class OrderServlet extends BaseServlet{
	//车型对象
	private CarModelEntity carmodel;
	private OrderService order;
	//计算天数
	private TimeUtils tu;
	//订单对象
	private CommonOrderEntity comorder;
	//实际租车表对象
	private RentEntity rent;
	//订单金额表
	private OrderMoney om;
	//时间表
	private TimeEntity time;
	private HashMap<String, Object> map;
	private SelfDrivingService sds;
	//private HashMap<String, Object> name;
	public OrderServlet() {
		carmodel = new CarModelEntity();
		order = new OrderService();
		tu = new TimeUtils();
		comorder = new CommonOrderEntity();
		rent = new RentEntity();
		om = new OrderMoney();
		time = new TimeEntity();
		map = new HashMap<>();
		sds = new SelfDrivingService();
		//name = new HashMap<>();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String carname = req.getParameter("carname");
		Double servicemoney=0.0;
		if(req.getParameter("kind").equals("到店取还")){
			//获取出发城市
			String fromcity = req.getParameter("fromCity");
			//获取到达城市
			String toCity = req.getParameter("toCity");
			//查询该城市的编号
			String fromnum = order.selStoreId(fromcity);
			String tonum = order.selStoreId(toCity);
			if(!fromnum.equals(tonum)){
				servicemoney = order.selServiceMoney(Integer.parseInt(fromnum), Integer.parseInt(tonum));
				
			}else{
				servicemoney=0.0;	
			}
			req.setAttribute("fromnum", fromnum);
			req.setAttribute("tonum", tonum);
		}
		
		req.setAttribute("servicemoney", servicemoney);
		//获取出发日期
		Date fromday = Date.valueOf(req.getParameter("fromDate"));
		//获取还车日期
		Date today = Date.valueOf(req.getParameter("toDate"));
		//计算天数
		int day = tu.getDaySub(fromday, today);
		//从数据库中获取车辆信息
		carmodel = order.getCarModel(carname);
		//获取套餐
		String time = req.getParameter("mon");
		Double timec = sds.getTimeCombo(time);
		req.setAttribute("day", day);
		req.setAttribute("carmodel", carmodel);
		req.setAttribute("timec", timec);
		//转发		
		forward("businessview/order");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//生成订单编号，格式“用户名+yyyyMMddHHmmss”
		java.util.Date d = new java.util.Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String ordernumber = req.getParameter("uname")+df.format(d);
		req.getSession().setAttribute("uname",req.getParameter("uname"));
		//存入订单表对象
		//1.订单编号
		comorder.setCo_id(ordernumber);
		//2.实际租车人身份证号
		comorder.setCo_rent_idcard(req.getParameter("id"));
		//3.车型编号
		comorder.setCo_car_type(req.getParameter("num"));
		//获取取送方式
		String qhkind = req.getParameter("qhkind");
		if(qhkind.equals("上门取还")){
			//4.取车地点（上门取送）
			comorder.setCo_get_address(req.getParameter("from"));
			//5.还车地点（上门取送）
			comorder.setCo_return_address(req.getParameter("to"));
			
		}else if(qhkind.equals("到店取还")){
			//4.取车门店编号
			comorder.setCo_get_store(req.getParameter("fromnum"));
			//5.还车门店编号
			comorder.setCo_return_store(req.getParameter("tonum"));
			
		}
		//6.订单状态
		comorder.setCo_order_status(req.getParameter("status"));
		//7.订单类型
		comorder.setCo_order_kind(req.getParameter("kind"));
		//8.应付金额
		comorder.setCo_plan_pay_money(Double.parseDouble(req.getParameter("money")));
		//9.取送方式
		comorder.setCo_get_kind(req.getParameter("qhkind"));
		if(qhkind.equals("上门取还")){
			//将该对象存入数据库(上门取还)
			order.insCommonOrder(comorder);
			
		}else if(qhkind.equals("到店取还")){
			//将该对象存入数据库(到店取还)
			order.insCommonOrderStore(comorder);
		}
		
		
		//存入实际租车人表
		//1.实际租车人姓名
		rent.setRent_name(req.getParameter("rname"));
		//2.租车人身份证号码
		rent.setRent_idcard(req.getParameter("id"));
		//3.租车人联系电话
		rent.setRent_telephone(req.getParameter("rtel"));
		//4.登录用户身份证号
		rent.setUser_idcard(req.getParameter("ucard"));
		//5.订单编号
		rent.setCo_id(ordernumber);
		//将该对象存入数据库
		order.insRent(rent);
		
		//存入订单金额表
		//1.订单编号
		om.setOrder_id(ordernumber);
		//2.租金
		om.setOrder_rent_money(Double.parseDouble(req.getParameter("rentcash")));
		//3.押金
		om.setOrder_guarantee_money(Double.parseDouble(req.getParameter("pledge")));
		//4.基本保险
		om.setOrder_insurance_money(Double.parseDouble(req.getParameter("insurance")));
		//5.手续费（到店取还的异店还车）
		if(qhkind.equals("到店取还")){
			if(!req.getParameter("fromnum").equals(req.getParameter("tonum"))){
				Double servicemoney = order.selServiceMoney(Integer.parseInt(req.getParameter("fromnum")), Integer.parseInt(req.getParameter("tonum")));
				om.setOrder_service_money(servicemoney);
				order.insOrderMoneyService(om);
			}else{
				order.insOrderMoney(om);
			}
			
		
		}else if(qhkind.equals("上门取还")){ 
			//将该对象存入数据库
			order.insOrderMoney(om);
		}
		
		
		//存入时间表
		//1.订单编号
		time.setTime_order_id(ordernumber);
		//2.预定取车时间
		Timestamp fromdate = new Timestamp(System.currentTimeMillis());
		fromdate=Timestamp.valueOf(req.getParameter("fromdate")+":00");
		time.setTime_plan_get(fromdate);
		
		//3.预定还车时间
		Timestamp todate = new Timestamp(System.currentTimeMillis());
		todate = Timestamp.valueOf(req.getParameter("todate")+":00");
		time.setTime_plan_return(todate);
		//4.订单提交时间
		SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ddd = dd.format(d);
		Timestamp subdate = new Timestamp(System.currentTimeMillis());
		subdate = Timestamp.valueOf(ddd);
		time.setTime_order_submit(subdate);
		//将该对象存入数据库
		order.insTime(time);
		
		//获取用户在页面选择的是“提交订单”，还是“支付全款”
		String zhifu = req.getParameter("zhifu");
				
		if(zhifu.equals("提交订单")){
			redirect("/mycenter");	
		}else if(zhifu.equals("支付全款")){
			map.put("ordernum", ordernumber);
			map.put("money", Double.parseDouble(req.getParameter("money")));
			redirect("/content/pay",map);	
		}
		
	}
}
